﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : MultiServerEngine | PhotonTestClient | PhotonListener.cs
// //  Created : 2016 - 02 - 16 20:53
// //  Modified: 2016 - 02 - 20 12:33
// //  ********************************************************************************

using System;
using System.Windows.Media;
using ExitGames.Client.Photon;

namespace PhotonTestClient
{
    public class PhotonListener : IPhotonPeerListener
    {
        public MainWindow Main;

        public void DebugReturn(DebugLevel level, string message)
        {
            Main.Dispatcher.Invoke(() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") +
                                              " - DebugReturn called with: " + level + " - " + message + "\n");
                Main.StatusTextBox.ScrollToEnd();
            });
        }

        public void OnOperationResponse(OperationResponse operationResponse)
        {
            Main.Dispatcher.Invoke(() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") +
                                              " - OnOperationResponse called with: " + operationResponse.OperationCode +
                                              " - " +
                                              operationResponse.ReturnCode + " - " + operationResponse.DebugMessage +
                                              "\n");

                foreach (var param in operationResponse.Parameters)
                {
                    Main.StatusTextBox.AppendText($"Parameter: {param.Key.ToString()} - Value: {param.Value.ToString()}\n");
                }

                Main.StatusTextBox.ScrollToEnd();
            });
        }

        public void OnStatusChanged(StatusCode statusCode)
        {
            Main.Dispatcher.Invoke(() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") +
                                              " - OnStatusChanged called with: " + statusCode + "\n");
                Main.StatusTextBox.ScrollToEnd();
                if (statusCode == StatusCode.Connect)
                {
                    Main.Disconnect_Button.IsEnabled = true;
                    Main.PeerConnected = true;
                }
                else
                {
                    Main.Disconnect_Button.IsEnabled = false;
                    Main.Connect_Button.IsEnabled = true;
                    Main.StatusIndicator.Fill = new SolidColorBrush(Colors.Red);
                    Main.PeerConnecting = false;
                    Main.PeerConnected = false;
                    Main.DispatcherTimer.Stop();
                }
            });
        }

        public void OnEvent(EventData eventData)
        {
            Main.Dispatcher.Invoke(() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") +
                                              " - OnEvent called with: " + eventData.Code);
                Main.StatusTextBox.ScrollToEnd();
            });
        }
    }
}
